package org.aeon.gamechatnest.service.impl;

import org.aeon.gamechatnest.common.response.RespStatus;
import org.aeon.gamechatnest.common.response.ResultBuild;
import org.aeon.gamechatnest.dto.UserVo;
import org.aeon.gamechatnest.exception.ApplicationException;
import org.aeon.gamechatnest.mapper.BlacklistMapper;
import org.aeon.gamechatnest.mapper.UserMapper;
import org.aeon.gamechatnest.pojo.Blacklist;
import org.aeon.gamechatnest.service.BlacklistService;
import org.aeon.gamechatnest.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 黑名单服务
 *
 * @author : Qik 2024/5/10 10:31
 */
@Service
public class BlacklistServiceImpl implements BlacklistService {

    @Autowired
    private BlacklistMapper blacklistMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = ApplicationException.class)
    public boolean save(Blacklist blacklist) {
        Long toUserId = blacklist.getToUser();

        // 获取登录用户id
        Long loginUserId = SecurityUtil.getUserId();

        // 校验非用户自己
        if (loginUserId.equals(toUserId)) {
            ResultBuild.result().state(RespStatus.PARAM_ERROR).message("不能添加自己为黑名单");
            return false;
        }
        // 校验目标用户是否存在
        if (userMapper.selectById(toUserId) == null) {
            ResultBuild.result().state(RespStatus.NOT_FOUND).message("目标用户不存在");
            return false;
        }
        // 检验不能重复添加
        if (blacklistMapper.getByFromAndTo(loginUserId, toUserId) != null) {
            ResultBuild.result().state(RespStatus.PARAM_ERROR).message("不能重复添加黑名单");
            return false;
        }

        // 创建黑名单
        blacklist.setFromUser(loginUserId);
        blacklist.setCreateTime(new Date());
        // 添加黑名单
        int added = blacklistMapper.add(blacklist);
        if (added != 1) {
            throw new ApplicationException(RespStatus.FAIL, "系统错误，添加黑名单失败");
        }

        return true;
    }

    @Override
    public boolean remove(Long id) {
        // 获取登录用户id
        Long loginUserId = SecurityUtil.getUserId();

        // 获取黑名单
        Blacklist blacklist = blacklistMapper.getById(id);
        // 校验黑名单存在
        if (blacklist == null) {
            ResultBuild.result().state(RespStatus.NOT_FOUND).message("请求数据不存在");
            return false;
        }
        // 校验黑名单属于用户
        if (!loginUserId.equals(blacklist.getFromUser())) {
            ResultBuild.result().state(RespStatus.NO_PERMISSION).message("黑名单不属于当前登录用户");
            return false;
        }

        // 删除黑名单
        int deleted = blacklistMapper.deleteById(id);
        if (deleted != 1) throw new ApplicationException(RespStatus.FAIL, "系统错误，删除黑名单失败");
        return true;
    }

    @Override
    public List<UserVo> findOwn() {
        return blacklistMapper.getByFrom(SecurityUtil.getUserId());
    }
}
